package com.dayaokeji.rhythmschoolstudent.client.common.exam;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ListFragment;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.UnTestedExamListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.ExamUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.api.ExamApi;
import com.dayaokeji.server_api.domain.Exam;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import retrofit2.Call;

/**
 * Created by Gorden on 2017/10/18.
 *
 * 题库列表
 */

public class ExamListFragment extends ListFragment<Exam,BaseViewHolder> {

    private String relId;
    private int relType;

    public static ExamListFragment newInstance(String relId, int relType) {

        Bundle args = new Bundle();
        args.putString(ExamActivity.EXTRA_REL_ID,relId);
        args.putInt(ExamActivity.EXTRA_REL_TYPE,relType);
        ExamListFragment fragment = new ExamListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        relId = getArguments().getString(ExamActivity.EXTRA_REL_ID);
        relType = getArguments().getInt(ExamActivity.EXTRA_REL_TYPE);

        setAdapter(new UnTestedExamListAdapter());

        setRecyclerView();

        setOnListener();
    }

    private void setRecyclerView() {
        RecyclerView recyclerView = getRecyclerView();
        if (recyclerView != null) {
            recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                    .colorResId(R.color.color_default_divider)
                    .sizeResId(R.dimen.default_divider_height)
                    .build());
            recyclerView.setBackgroundColor(Color.WHITE);
        }
    }

    private void setOnListener() {
        BaseQuickAdapter<Exam, BaseViewHolder> adapter = getAdapter();
        if (adapter != null) {
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    int size = adapter.getData().size();
                    if (position < size) {
                        Exam exam = (Exam) adapter.getData().get(position);
                        if (exam.getStatus() == 3) {
                            Intent intent = new Intent(getActivity(), AnswerActivity.class);
                            intent.putExtra(AnswerActivity.EXTRA_EXAM_ID,exam.getId());
                            intent.putExtra(AnswerActivity.EXTRA_SHOW_EXAM_RESULT,false);
                            startActivity(intent);
                        }else if (exam.getStatus() == 5) { // 已批阅
                            Intent intent = new Intent(getActivity(), AnswerActivity.class);
                            intent.putExtra(AnswerActivity.EXTRA_EXAM_ID,exam.getId());
                            intent.putExtra(AnswerActivity.EXTRA_SHOW_EXAM_RESULT,true);
                            startActivity(intent);
                        }else{
                            ToastUtils.info(ExamUtils.getStatusName(exam.getStatus()));
                        }
                    }
                }
            });
        }
    }

    @Override
    protected Call createCall(int offset, int max) {
        return ApiUtils.getApi(ExamApi.class).list(relId,relType);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
